using System.Xml.Serialization;
using DG.Tweening;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;

    private int _coinCount;//金币数
    public int CoinCount{
        get { return _coinCount; }
        set{
            _coinCount = value;
            //更新金币文本
            PlayerEvents.UpdateCoin(_coinCount);
        }
    }

    public GameObject gameOverUI;

    [HideInInspector] public float currentHealth; //玩家血量

    [Header("伤害飘字")]
    public GameObject uiShowText;//伤害飘字预制体

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else if (Instance != this)
        {
            Destroy(gameObject);
        }

        DontDestroyOnLoad(gameObject);
    }

    private void Start() {
        CoinCount = _coinCount;
        currentHealth = PlayerCharacter.Instance.maxHealth;
        gameOverUI.SetActive(false);
        AudioManager.Instance.PlayMusic("BGM");
    }

    //伤害飘字
    public void ShowText(string str, Vector2 pos, Color color)
    {
        GameObject text = Instantiate(uiShowText, pos, Quaternion.identity);
        text.GetComponent<UIShowText>().SetText(str, color);
    }

    //改变金币数量
    public void ChangeCoins(int amount)
    {
        if (CoinCount + amount < 0)
        {
            Debug.Log("金币不足");
            return;
        }
        CoinCount += amount;
    }

    //保存数据
    public void Save()
    {
        //保存玩家血量
        currentHealth = PlayerCharacter.Instance.CurrentHealth;
    }

    //加载数据
    public void Load()
    {
        //更新金币
        CoinCount = _coinCount;
        AudioManager.Instance.PlayMusic("BGM");
    }

    //游戏结束
    public void GameOver(){
        gameOverUI.SetActive(true);
    }

    //重新开始
    public void GameRestart(){
        //回满血
        PlayerCharacter.Instance.CurrentHealth = PlayerCharacter.Instance.maxHealth;
        AudioManager.Instance.PlayMusic("BGM");
        gameOverUI.SetActive(false);
        SceneManager.LoadScene("Game");
        //清除 DOTween 库中当前正在进行的所有动画和补间
        DOTween.Clear();
    }
}